home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / WUNZ20SR.ZIP / ACTION.C < prev    next >
C/C++ Source or Header  |  1993-06-08  |  13KB  |  394 lines

  1. #include "wizunzip.h"
  2. #include "unzip.h"     
  3.  
  4. /* Action.c module of WizUnZip.
  5.  * Author: Robert A. Heath, 1993
  6.  * I, Robert Heath, place this source code module in the public domain.
  7.  */
  8. char __based(__segname("STRINGS_TEXT")) szNoMemory[] = 
  9.             "Insufficient memory for this operation!";
  10.  
  11. char __based(__segname("STRINGS_TEXT")) szCantChDir[] = 
  12.             "Can't change directory to %s!";
  13.  
  14.  
  15. /* Get Selection Count returns a count of the selected 
  16.  * list box items. If the count is  greater than zero, it also returns
  17.  * a pointer to a locked list in local memory of the item nos.
  18.  * and their local memory handle.
  19.  * A value of -1 indicates an error.
  20.  */
  21. int CLBItemsGet(HWND hListBox, int __far * __far *ppnSelItems, HANDLE *phnd)
  22. {
  23.     int cSelLBItems = (int)SendMessage(hListBox, LB_GETSELCOUNT, 0, 0L);
  24.  
  25.     if ( !phnd )
  26.         return -1;
  27.  
  28.     *phnd = 0;
  29.     if (cSelLBItems)
  30.     {
  31.         *phnd = GlobalAlloc(GMEM_FIXED, cSelLBItems * sizeof(int));
  32.         if ( !*phnd )
  33.             return -1;
  34.  
  35.         *ppnSelItems = (int __far *)GlobalLock( *phnd );
  36.         if ( !*ppnSelItems )
  37.         {
  38.             GlobalFree( *phnd );
  39.             *phnd = 0;
  40.             return -1;
  41.         }
  42.  
  43.         if (SendMessage(hWndList, LB_GETSELITEMS, cSelLBItems, (LONG)*ppnSelItems) != cSelLBItems)
  44.         {
  45.             GlobalUnlock(*phnd);
  46.             GlobalFree(*phnd);
  47.             *phnd = 0;
  48.             return -1;
  49.         }
  50.     }
  51.     return cSelLBItems;
  52. }
  53.  
  54. /* Re-select listbox contents from given list. The pnSelItems is a
  55.  * list containing the indices of those items selected in the listbox.
  56.  * This list was probably created by GetLBSelCount() above.
  57.  */
  58. void ReselectLB(HWND hListBox, int cSelLBItems, int __far *pnSelItems)
  59. {
  60.     int i;
  61.  
  62.     
  63.     for (i = 0; i < cSelLBItems; ++i)
  64.     {
  65.         SendMessage(hListBox, LB_SETSEL, TRUE, MAKELPARAM(pnSelItems[i],0));
  66.     }
  67. }
  68.  
  69.  
  70. #define cchFilesMax 1024
  71.  
  72. /* Action is called on double-clicking, or selecting one of the 3
  73.  * main action buttons. The action code is the action
  74.  * relative to the listbox or the button ID.
  75.  */
  76. void Action(HWND hWnd, WORD wActionCode)
  77. {
  78.     HANDLE  hMem = 0;
  79.     int i;
  80.     int iSelection;
  81.     int cch;
  82.     int cchCur;
  83.     int cchTotal;
  84.     int __far *pnSelItems;  /* pointer to list of selected items */
  85.     HANDLE  hnd = 0;
  86.     int cSelLBItems = CLBItemsGet(hWndList, &pnSelItems, &hnd);
  87.     int argc;
  88.     LPSTR   lpszT;
  89.     char **pszIndex;
  90.     char *sz;
  91.     WORD wIndex = !uf.fFormatLong ? SHORT_FORM_FNAME_INX
  92.                             : LONG_FORM_FNAME_INX;
  93.  
  94.     gfCancelDisplay = FALSE;    /* clear any previous cancel */
  95.     /* if no items were selected */
  96.     if (cSelLBItems < 1)
  97.         return;
  98.  
  99.     /* Note: this global value can be overriden in replace.c */
  100.     uf.fDoAll = (uf.fOverwrite) ? 1 : 0;
  101.  
  102.     SetCapture(hWnd);
  103.     hSaveCursor = SetCursor(hHourGlass);
  104.     ShowCursor(TRUE);
  105.  
  106. #if 1
  107.     /* If all the files are selected pass in no filenames */
  108.     /* since unzipping all files is the default */
  109.     hMem = GlobalAlloc( GPTR, 4096 );
  110.     if ( !hMem )
  111.         goto done;
  112.     lpszT = (LPSTR)GlobalLock( hMem );
  113.     if ( !lpszT )
  114.     {
  115.         GlobalFree( hMem );
  116.         goto done;
  117.     }
  118.  
  119.     argc = ((WORD)cSelLBItems == cZippedFiles) ? 0 : 1;
  120.     iSelection = 0;
  121.  
  122.     do
  123.     {
  124.         char rgszFiles[cchFilesMax];
  125.  
  126.         if (argc)
  127.         {
  128.             cchCur = 0;
  129.             pszIndex = (char **)rgszFiles;
  130.             cch = (sizeof(char *) * ((cSelLBItems > (cchFilesMax/16)-1 ) ? (cchFilesMax/16) : cSelLBItems+1));
  131.             cchTotal = (cchFilesMax-1) - cch;
  132.             sz = rgszFiles + cch;
  133.             
  134.             for (i=0; ((i+iSelection)<cSelLBItems) && (i<(cchFilesMax/16)-1); ++i)
  135.             {
  136.                 cch = (int)SendMessage(hWndList, LB_GETTEXTLEN, pnSelItems[i+iSelection], 0L);
  137.                 if (cch != LB_ERR)
  138.                 {
  139.                     if ((cchCur+cch+1-wIndex) > cchTotal)
  140.                         break;
  141.                     cch = (int)SendMessage(hWndList, LB_GETTEXT, pnSelItems[i+iSelection], (LONG)lpszT);
  142.                     if ((cch != LB_ERR) && (cch>wIndex))
  143.                     {
  144.                         lstrcpy(sz, lpszT+wIndex);
  145.                         pszIndex[i] = sz;
  146.                         cchCur += (cch + 1 - wIndex);
  147.                         sz += (cch + 1 - wIndex);
  148.                     }
  149.                     else
  150.                     {
  151.                         break;
  152.                     }
  153.                 }
  154.                 else
  155.                 {
  156.                     MessageBeep(1);
  157.                     goto done;
  158.                 }
  159.             }
  160.             if (i == 0)
  161.                 goto done;
  162.             argc = i;
  163.  
  164.             pszIndex[i] = 0;
  165.             iSelection += i;
  166.         }
  167.         else
  168.         {
  169.             iSelection = cSelLBItems;
  170.         }
  171.  
  172.         switch (wActionCode)
  173.         {
  174.         case 0:         /* extract */
  175.             if (FSetUpToProcessZipFile(0, 0, 0, 1, 0, 
  176.                             (int)(uf.fRecreateDirs ? 1 : 0), 
  177.                             uf.fDoAll, (int)(uf.fTranslate ? 1 : 0),
  178.                                 argc, lpumb->szFileName, (char **)rgszFiles))
  179.             {
  180.             int DlgDirListOK = 1; /* non-zero when DlgDirList() succeeds */
  181.  
  182.                 /* If extracting to different directory from archive dir.,
  183.                  * temporarily go to "unzip to" directory.
  184.                  */ 
  185.                 if (!uf.fUnzipToZipDir && lpumb->szUnzipToDirName[0])
  186.                 {
  187.                     lstrcpy(lpumb->szBuffer, lpumb->szUnzipToDirName); /* OK to clobber szBuffer! */
  188.                     DlgDirListOK = DlgDirList(hWnd, lpumb->szBuffer, 0, 0, 0);
  189.                 }
  190.                 if (!DlgDirListOK)    /* if DlgDirList failed  */
  191.                 {
  192.                     wsprintf(lpumb->szBuffer, szCantChDir, lpumb->szUnzipToDirName);
  193.                     MessageBox(hWndMain, lpumb->szBuffer, NULL, MB_OK | MB_ICONEXCLAMATION);
  194.                 }
  195.                 else
  196.                 {
  197.                     process_zipfile();    /* extract the file(s)            */
  198.                     /* Then return to archive dir. after extraction.
  199.                      * (szDirName is always defined if archive file defined)
  200.                      */
  201.                     if (!uf.fUnzipToZipDir && lpumb->szUnzipToDirName[0])
  202.                     {
  203.                         lstrcpy(lpumb->szBuffer, lpumb->szDirName); /* OK to clobber szBuffer! */
  204.                         if (!DlgDirList(hWnd, lpumb->szBuffer, 0, 0, 0)) /* cd back */
  205.                         {
  206.                             wsprintf(lpumb->szBuffer, szCantChDir, lpumb->szDirName);
  207.                             MessageBox(hWndMain, lpumb->szBuffer, NULL, MB_OK | MB_ICONEXCLAMATION);
  208.  
  209.                         }
  210.                     }
  211.                 }
  212.             }
  213.             else
  214.             {
  215.                 MessageBox(hWndMain, szNoMemory, NULL, MB_OK | MB_ICONEXCLAMATION);
  216.             }
  217.             TakeDownFromProcessZipFile();
  218.             break;
  219.         case 1:     /* display to message window */
  220.             bRealTimeMsgUpdate = FALSE;
  221.             if (FSetUpToProcessZipFile(1, 0, 0, 1, 0,  0, 0, 0,
  222.                                 argc, lpumb->szFileName, (char **)rgszFiles))
  223.             {
  224.                 process_zipfile();
  225.             }
  226.             else
  227.             {
  228.                 MessageBox(hWndMain, szNoMemory, NULL, MB_OK | MB_ICONEXCLAMATION);
  229.             }
  230.  
  231.             TakeDownFromProcessZipFile();
  232.             bRealTimeMsgUpdate = TRUE;
  233.             if (uf.fAutoClearStatus)    /* if automatically clearing status, leave user at top */
  234.                 SetStatusTopWndPos();
  235.     
  236.             else    /* traditional behavior leaves user at bottom of window */
  237.                 UpdateMsgWndPos();
  238.  
  239.             /* Following extraction to status window, user will want
  240.              * to scroll around, so leave him/her on Status window.
  241.              */
  242.             if (wWindowSelection != IDM_MAX_LISTBOX)
  243.                 PostMessage(hWnd, WM_COMMAND, IDM_SETFOCUS_ON_STATUS, 0L);
  244.  
  245.             break;
  246.         case 2:     /* test */
  247.             if (FSetUpToProcessZipFile(0, 1, 0, 1, 0,  0, 0, 0,
  248.                                 argc, lpumb->szFileName, (char **)rgszFiles))
  249.             {
  250.                 process_zipfile();
  251.             }
  252.             else 
  253.             {
  254.                 MessageBox(hWndMain, szNoMemory, NULL, MB_OK | MB_ICONEXCLAMATION);
  255.